/**
 * 菜单编辑、新增
 *
 * @author liuxiaotang
 */
define(["app", "js/services/MessageBox", "js/services/MuseService"], function (app) {

    var deps = ["$scope", "$state", "$modal", "$stateParams", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, $stateParams, MessageBox, MuseService) {
        //获取参数
        var menuId = $stateParams.menuId || '';
        var parentId = $stateParams.parentId || '';

        //获取数据
        $scope.getDetail = function () {
            MuseService.get(baseServices_restfulUrl + "admin/adminmenu/load.json?id=" + menuId).success(
                function (data, status, headers, config) {
                    console.log(data);
                    if (data.state == 0) {
                        $scope.menu = data.data;
                        console.log($scope.menu)
                    } else {
                        MessageBox.notify('获取信息失败：' + data.message);
                    }
                });
        };
        if (menuId) {
            $scope.getDetail();
        }

        //保存
        $scope.saveObj = function () {
            if (parentId && parentId != '') {
                $scope.menu.parent = {
                    'id': parentId
                }
            }
            MuseService.postObj(baseServices_restfulUrl + "admin/adminmenu/save.json", $scope.menu).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.result = data.data;
                        MessageBox.notify('保存成功!');
                        //刷新左侧菜单
                        $scope.getMenuList();
                        $state.go('adminmenuList');
                    } else {
                        MessageBox.notify('保存失败：' + data.message);
                    }
                });
        };

        // 取消，返回
        $scope.close = function () {
            $state.go("adminmenuList");
        };
    }

    controller.$inject = deps;
    app.lazy.controller("AdminMenuEditController", controller);
});
